<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="NFT-ID">
          <el-input v-model="form.nft_id" placeholder="NFT-ID" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" width="150" />
          <el-table-column
            prop="uid"
            label="用户UID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="nft_id"
            label="NFT-ID"
            align="center"
            width="150"
          />

          <el-table-column
            prop="income_name"
            label="收益名称"
            align="center"
            width="150"
          />
          <el-table-column
            prop="issue_price"
            label="发行价(钻石)"
            align="center"
            width="150"
          />
          <el-table-column
            prop="income"
            label="收益"
            align="center"
            width="150"
          />
          <el-table-column
            prop="income_rate"
            label="收益比例"
            align="center"
            width="150"
          />
          <el-table-column
            prop="source"
            label="来源记录"
            align="center"
            width="150"
          />
          <el-table-column
            prop="source_id"
            label="来源记录ID"
            align="center"
            width="150"
          />

          <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="150"
            :formatter="formatterStatus"
          />
          <el-table-column
            prop="createtime"
            label="创建时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            prop="updatetime"
            label="更新时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatterTime } from "@/utils/utils";
import { incomeApi } from "@/api/moby/diamond/diamond";

const loading = ref(false);
const tableData = ref([]);

const formData = {
  uid: "",
  nft_id: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    nft_id: form.nft_id,
    uid: form.uid,
  };
};

const formatterStatus = (row, column, cellValue, index) => {
  let obj = {
    0: " 待确认",
    1: " 成功",
    2: " 失败",
  };
  return obj[cellValue];
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await incomeApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await incomeApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    nft_id: "NFT ID",
    income_name: "收益名称",
    issue_price: "发行价(钻石)",
    income: "收益",
    income_rate: "收益比例",
    source: "来源记录",
    source_id: "来源记录ID",
    status: "状态",
    createtime: "创建时间",
    updatetime: "更新时间",
  };
  exportToExcel(res.data.results, head, "Moby-钻石收益记录");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
